package cn.jietuo.juc.c_005_volatile;

import java.util.concurrent.TimeUnit;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/5/4
 * @description: volatile初识
 */
public class T01_HelloVolatile {

    // 如果不加这个volatile，此程序就不会停下来
    volatile  boolean isRunning = true;

    void m() {
        System.out.println("m start");
        while (isRunning) {

        }
        System.out.println("m end");
    }

    public static void main(String[] args) {
        T01_HelloVolatile t01_helloVolatile = new T01_HelloVolatile();
        new Thread(() -> {
            t01_helloVolatile.m();
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t01_helloVolatile.isRunning = false;
    }
}
